/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			aspaceid.cc
  \date			Sep 2012
  \author		TNick

  \brief		Contains the implementation of ASpaceID class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<cpg/cpg.h>

#include	<usermsg.h>

#include	<cpg/logic/cpgdoc.h>
#include	<cpg/logic/pgspace.h>

#include	"aspaceid.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace cpg;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */


/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
ASpaceID::ASpaceID	( CpgDoc * doc )
	: AtomicChg( doc )
{
	/* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ASpaceID::~ASpaceID	( void )
{
	if ( isDeleteVariant() )
	{
		delete new_space_;
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					ASpaceID::doUndoRedo				( bool b_effect )
{
	if ( b_effect )
	{ /* redo for create, undo for delete */
		document()->addSpace( new_space_ );
		new_space_->raiseSpaceCreated();
	}
	else
	{ /* undo for create, redo for delete */
		document()->remSpace( new_space_ );
		new_space_->raiseSpaceDeleted();
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ASpaceID *				ASpaceID::createSpace				(
		BBlocks::UserMsg & um, CpgDoc * doc, PgSpace * space )
{
	/* get current document if none was provided */
	if ( doc == NULL )
	{
		doc = CpgLibrary::crtDoc();
		if ( doc == NULL )
		{
			um.add( BBlocks::UserMsgMgr::MSG_ERROR, QObject::tr(
						"There is no current document to insert a space into.\n" )
					);
			return NULL;
		}
	}

	/* create a new, default space if none was provided */
	if ( space == NULL )
	{
		space = new PgSpace( doc->spaceUniqName() ); /* creates default values */
	}

	/* create a new instance of this atom */
	ASpaceID *		ret = new ASpaceID( doc );
	ret->setLongDescr( QObject::tr( "Created space %1" ).arg( space->name() ) );

	/*	storing the space in space_ member of AtomicChg would be a mistake
	*	because that space indicates the context of the command. The manager
	*	may try to activate it prior to or after the undo/redo action.
	*/
	ret->new_space_ = space;

	/* perform this action */
	ret->pushMe();
	return ret;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ASpaceID *				ASpaceID::deleteSpace				(
		BBlocks::UserMsg & um, CpgDoc * doc, PgSpace * space)
{

	/* get current document if none was provided */
	if ( doc == NULL )
	{
		doc = CpgLibrary::crtDoc();
		if ( doc == NULL )
		{
			um.add( BBlocks::UserMsgMgr::MSG_ERROR, QObject::tr(
						"There is no current document to delete a space from.\n" )
					);
			return NULL;
		}
	}

	/* get current document if none was provided */
	if ( space == NULL )
	{
		space = doc->currentSpace();
		if ( space == NULL )
		{
			um.add( BBlocks::UserMsgMgr::MSG_ERROR, QObject::tr(
						"There is no current space in the document.\n" )
					);
			return NULL;
		}
	}

	ASpaceID *		ret = new ASpaceID( doc );
	ret->setReversed();
	ret->setLongDescr( QObject::tr( "Deleted space %1" ).arg( space->name() ) );

	/*	storing the space in space_ member of AtomicChg would be a mistake
	*	because that space indicates the context of the command. The manager
	*	may try to activate it prior to or after the undo/redo action.
	*/
	ret->new_space_ = space;

	ret->pushMe();
	return ret;

}
/* ========================================================================= */


/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
